package com.jeecms.wicool.dao.car.impl;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarAdviserDao;
import com.jeecms.wicool.entity.car.CarAdviser;

@Repository
public class CarAdviserDaoImpl extends HibernateBaseDao<CarAdviser, Integer>
		implements CarAdviserDao {
	HttpServletRequest request;
	@Override
	protected Class<CarAdviser> getEntityClass() {
		// TODO Auto-generated method stub
		return CarAdviser.class;
	}
	@Override
	public Pagination getPage(String name,Short level,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from CarAdviser bean where 1=1 and bean.id not in(1,2) ");
			if (!StringUtils.isBlank(name)){
				f.append(" and bean.name like :name");
				f.setParam("name", "%" + name + "%");
			}if(level!=null){
				f.append(" and bean.level = :level");
				f.setParam("level", level);
			}
			if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			
			f.append(" order by bean.orderNum desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public CarAdviser findById(Integer id) {
		CarAdviser entity = get(id);
		return entity;
	}
	@Override
	public CarAdviser save(CarAdviser bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public CarAdviser deleteById(Integer id) {
		CarAdviser entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public List<CarAdviser> getAll(CmsUser user) {
		Finder f= Finder.create("select bean from CarAdviser bean where 1=1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
}